(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
	var uid = userinfo.user_id;
	var release_money = 0;

	get_data();
	/*
	    if(!userinfo.out_address || !userinfo.privatekey || !userinfo.mnemonic) {
			var wallet = ethers.Wallet.createRandom();
			wallet.user_id = uid;
			//console.log(JSON.stringify(wallet));
			mui.ajax({
				url: purseAddrurl,
				data: wallet,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					userinfo.out_address = res.out_address;
					userinfo.privatekey = res.privatekey;
					userinfo.mnemonic = res.mnemonic;
					app.setState(userinfo);
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
		}
        */

	$.plusReady(function() {
		//仅支持竖屏显示
		plus.screen.lockOrientation("portrait-primary");

	})

	function get_data() {
		mui.getJSON(apiurl + "User/get_user/", {
			'user_id': uid,
			'_token': userinfo.token
		}, function(res) {
			console.log(JSON.stringify(res));
			if(res.status == -1 || res.status == -3) {
				mui.toast(res.msg);
				app.out();
				return false;
			}

			userinfo.new_vesionid = res.vesion.version;
			userinfo.upload_url = res.vesion.url;
			userinfo.share_url = res.vesion.share_url;
			userinfo.a1_money = res.data.a1_money;
			userinfo.a2_money = res.data.a2_money;
			userinfo.current_price = res.data.current_price;
			app.setState(userinfo);

			document.getElementById('remaining').innerHTML = res.data.a1_money;
			document.getElementById('integral').innerHTML = res.data.a2_money;
			document.getElementById('index_note').innerHTML = res.index_notice;

			if(res.release_money > 0) {
				release_money = res.release_money;
				var width = document.documentElement.clientWidth;
				var height = document.documentElement.clientHeight;
				document.querySelector('#packet .red_packet').style.width = width + 'px';
				document.querySelector('#packet .red_packet').style.height = height + 'px';
				doc.getElementById('packet').style.display = 'block';
				doc.getElementById('packet_num').innerHTML = res.release_money;
			} else {
				release_money = 0;
				doc.getElementById('packet').style.display = 'none';
			}
		});
	}

	//点击领取红包
	doc.getElementById('do_release').addEventListener('tap', function() {
		mui.ajax({
			url: apiurl + "User/do_release/",
			data: {
				'user_id': uid,
				'money': release_money,
				'_token': userinfo.token
			},
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'post',
			timeout: 10000,
			success: function(res) {
				console.log(JSON.stringify(res));
				if(res.status == -3) {
					mui.toast(res.msg);
					app.out();
					return false;
				}

				// 请求成功
				if(res.status < 1) {
					mui.toast(res.msg);
				} else {
					mui.toast('领取红包成功');
					userinfo.a1_money = res.data.a1_money;
					userinfo.a2_money = res.data.a2_money;
					app.setState(userinfo);
					//doc.getElementById('remaining').innerHTML = res.data.a1_money;
					//doc.getElementById('integral').innerHTML = res.data.a2_money;
					doc.getElementById('packet').style.display = 'none';
					doc.getElementById('packet_num').innerHTML = '';
					mui.fire(plus.webview.getWebviewById('wallet.html'), 'refresh_data');
					location.href = 'index.html';

				}
			},
			error: function(xhr, type, errorThrown) {
				//console.log(JSON.stringify(xhr));
				//console.log(JSON.stringify(type));
				//console.log(JSON.stringify(errorThrown));
				mui.toast('操作失败！');
			}
		});
	});

	//扫码支付
	/*
	doc.getElementById('saoma').addEventListener('tap', function() {
		plus.webview.open('/pages/weep.html','weep');
	}, false);
	*/

	doc.getElementById('convert').addEventListener('tap', function() {
		//打开转入页
		mui.openWindow({
			id: 'convert.html',
			url: '/pages/order/convert.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		});
	}, false);

	doc.getElementById('wallet_zichan').addEventListener('tap', function() {
		//打开转入页
		mui.openWindow({
			id: 'wallet.html',
			url: 'wallet.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		});
	}, false);

	doc.getElementById('community').addEventListener('tap', function() {
		var hichatUrl = 'https://service.hichat.chat/hichat.apk';
		plus.runtime.openURL(hichatUrl);
	}, false);

	document.getElementById("shangxueyuan").addEventListener('tap', function() {
		//商学院
		mui.openWindow({
			url: '/pages/sxy/sxy.html',
			id: '/pages/sxy/sxy.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		})

	});

	document.getElementById("chuanqibao").addEventListener('tap', function() {
		//传奇宝
		/*
			mui.alert('敬请期待...', '传奇先锋', function() {
				
			});
			*/
		mui.openWindow({
			url: 'pages/chuanqibao.html',
			id: 'pages/chuanqibao.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		})
	});

	document.getElementById("jiaoyisuo").addEventListener('tap', function() {
		//交易区
		// 			mui.alert('敬请期待...', '传奇先锋', function() {
		// 				
		// 			});
		mui.openWindow({
			url: 'pages/exchange.html',
			id: 'pages/exchange.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		})
		// mui.toast('惊不惊喜，意不意外')
	});

	document.getElementById("chuangkequ").addEventListener('tap', function() {
		//创客区
		mui.openWindow({
			url: '/pages/ckq/ckq_home.html',
			id: '/pages/ckq/ckq_home.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		})

	});

	document.getElementById("zimeiti").addEventListener('tap', function() {
		//传字媒体
		mui.alert('敬请期待...', '传奇先锋', function() {

		});
	});

	document.getElementById("taobao").addEventListener('tap', function() {
		//淘宝
		mui.alert('敬请期待...', '传奇先锋', function() {

		});
	});

	document.getElementById("xianxiashanghu").addEventListener('tap', function() {
		//线下商户
		mui.openWindow({
			url: '/pages/lineMer/merHome.html',
			id: '/pages/lineMer/merHome.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		})

	});

	document.getElementById("xiangmufuhua").addEventListener('tap', function() {
		//项目孵化
		mui.openWindow({
			url: '/pages/xmfh/xmhf_home.html',
			id: '/pages/xmfh/xmhf_home.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		})

	});

	document.getElementById("shenghuofuwu").addEventListener('tap', function() {
		//传生活服务
		mui.alert('敬请期待...', '传奇先锋', function() {

		});
	});

	document.getElementById("gongyi").addEventListener('tap', function() {
		//公益
		mui.alert('敬请期待...', '传奇先锋', function() {

		});
	});

	document.getElementById("youxi").addEventListener('tap', function() {
		//游戏
		mui.alert('敬请期待...', '传奇先锋', function() {

		});
	});

	doc.getElementById('rollin').addEventListener('tap', function() {
		//打开转入页
		mui.openWindow({
			id: 'rollin.html',
			url: '/pages/order/roll_in.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		});
	}, false);

	doc.getElementById('roll_out').addEventListener('tap', function() {
		//打开转出页
		mui.openWindow({
			id: 'rollout.html',
			url: '/pages/order/roll_out.html',
			show: {
				aniShow: 'pop-in'
			},
			waiting: {
				autoShow: false
			}
		});
	}, false);

	doc.getElementById('balance_record').addEventListener('tap', function() {
		//打开余额记录页
		mui.openWindow({
			id: 'balance_record.html',
			url: '/pages/balance_record.html',
			show: {
				autoShow: false
			},
			waiting: {
				autoShow: true,
				title: '正在加载...'
			}
		});
	}, false);

	doc.getElementById('record_point').addEventListener('tap', function() {
		//打开节点记录页
		mui.openWindow({
			id: 'record_point.html',
			url: '/pages/record_point.html',
			show: {
				autoShow: false
			},
			waiting: {
				autoShow: true,
				title: '正在加载...'
			}
		});
	}, false);

	window.addEventListener('refresh_amoney', function(event) {
		userinfo = app.getState() || {};
		doc.getElementById('remaining').innerHTML = userinfo.a1_money;
		doc.getElementById('integral').innerHTML = userinfo.a2_money;

	});

}(mui, document));